package com.easygem.business.service.imp;

import com.easygem.business.mapper.AccountMapper;
import com.easygem.business.service.AccountService;
import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.model.business.Account;
import com.easygem.common.model.system.User;
import com.easygem.common.vo.business.AccountVO;
import com.easygem.common.vo.business.ClientVO;
import com.easygem.common.vo.business.SysInformationVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.system.util.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @author 何维佳
 * @version 1.0
 * @className AccountServiceImpl
 * @Description TODO
 * @date 2025/1/13 15:19
 */
@Service
@Transactional
public class AccountServiceImpl extends AbstractServiceImpl<AccountMapper, Account, Integer> implements AccountService {

    @Autowired
    AccountMapper accountMapper;

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param accountVO
     * @return
     */
    @Override
    public PageVO<Account> findList(Integer pageNum, Integer pageSize, AccountVO accountVO) {
        PageHelper.startPage(pageNum,pageSize);
        Account account = new Account();
        BeanUtils.copyProperties(accountVO,account);
        List<Account> accountList = accountMapper.selectAccountListByAccountList(account);
        PageInfo<Account> pageInfo = new PageInfo<>(accountList);
        return new PageVO<>(pageInfo.getTotal(),accountList);
    }


    @Override
    @Transactional
    public int update(AccountVO accountVO) {
        int result = accountMapper.updateAccount(accountVO);
        return result;
    }

    @Override
    @Transactional
    public int delete(Long Id) {
        int result = accountMapper.deleteAccountById(Id);
        return result;
    }

    @Override
    @Transactional
    public int deleteUserByAccountName(String accountName) {
        int result = accountMapper.deleteUserByAccountName(accountName);
        accountMapper.deleteInfoByUserStatus();
        return result;
    }

    @Override
    @Transactional
    public int updateUserByAccountName(String accountName) {
        int result = accountMapper.updateUserByAccountName(accountName);
        accountMapper.updateInfoByUserStatus();
        return result;
    }


    @Override
    public int findAccountName(String accountName) {
        return accountMapper.findAccountName(accountName);
    }

    @Override
    @Transactional
    public int addAccount(AccountVO accountVO) {

        ClientVO clientVO = new ClientVO();
        clientVO.setPhone(accountVO.getPhone());
        clientVO.setLinkman(accountVO.getLinkman());
        clientVO.setClientName(accountVO.getClientName());
        clientVO.setClientNo(accountVO.getClientNo());
        clientVO.setEmployeeId_aftermarket(accountVO.getEmployeeId_aftermarket());
        clientVO.setClientTypeId(accountVO.getClientTypeId());
        clientVO.setCompany(accountVO.getCompany());

        User userVO = new User();
        userVO.setPhone(accountVO.getPhone());
        userVO.setDeptId(accountVO.getDeptId());
        userVO.setAccountName(accountVO.getAccountName());
        String salt= UUID.randomUUID().toString().substring(0,32);
        userVO.setPassword(MD5Utils.md5Encryption("123456", salt));
        userVO.setEditTime(new Timestamp(System.currentTimeMillis()));
        userVO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        userVO.setSalt(salt);
        userVO.setType(0);
        userVO.setStatus(1);//添加的用户默认启用
        SysInformationVO sysInformationVO = new SysInformationVO();
        sysInformationVO.setAccountName(accountVO.getAccountName());


        Long userResult = accountMapper.insertUser(userVO);

        Long newId = accountMapper.getUserNewId();

//        Long userId = accountMapper.selectUserIdByUser(userVO);
//
        int userRoleInsertResult = accountMapper.insertUserRole(newId);

        int clientInsertResult = accountMapper.insertClient(clientVO);
        // 获取 bg_client 自增主键 id 的逻辑
//        int clientId = accountMapper.selectClientIdByClient(clientVO);
        // 假设 getClientId 方法可以根据 clientVO 的某些信息从数据库中查询并返回 bg_client 的自增主键 id
        // 这里可能需要根据你的具体数据库操作实现，例如使用 MyBatis 的 selectKey 或者使用 JDBC 的 getGeneratedKeys 等
        Integer newId2 = accountMapper.getClientNewId();
        accountVO.setClientId(newId2);
        accountVO.setIsZoning(0);
        int accountInsertResult = accountMapper.insertAccount(accountVO);
        int sysInformation = accountMapper.insertSysInformation(sysInformationVO);
        if (accountInsertResult > 0 && clientInsertResult > 0 && userResult!= 0 && sysInformation > 0 && userRoleInsertResult>0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int chackCircuritIdIsChange(AccountVO accountVO) {
        return accountMapper.chackCircuritIdIsChange(accountVO);
    }

    @Override
    @Transactional
    public int updateUserCircuritIdByAccountName(AccountVO accountVO) {
        return accountMapper.updateUserCircuritIdByAccountName(accountVO);
    }
}
